package djrain.lilochat.server;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * Hello world!
 * 
 */
public class Server extends JPanel
{
	private static final long serialVersionUID = 1L;

	private JTextArea textArea = new JTextArea();
	private JScrollPane scrollPanel = new JScrollPane(textArea);

	private ServerSocket serverSocket = null;
	private Socket socket = null;

	private Vector<Client> clients = new Vector<Client>();

	public Server()
	{

		this.setLayout(new BorderLayout(5, 5));

		this.setPreferredSize(new Dimension(350, 400));

		add(scrollPanel, BorderLayout.CENTER);

	}

	public void broadcast(String str)
	{
		textArea.append("B>" + str + "\n");
		for (int i = 0; i < clients.size(); i++)
		{
			textArea.append("B,[" + clients.elementAt(i).getPort() + "]>" + str + "\n");
			clients.elementAt(i).sendMessage(str);
		}
	}

	public void broadcastExcept(Client c, String str)
	{
		textArea.append("O," + c.getPort() + ">[" + c.getUsername() + "]" + str + "\n");

		for (int i = 0; i < clients.size(); i++)
		{
			if (c.equals(clients.elementAt(i)))
				continue;
			clients.elementAt(i).sendMessage(str);
		}
	}

	public void messageTo(Client c, String str)
	{
		textArea.append("S," + c.getPort() + ">[" + c.getUsername() + "]" + str + "\n");
		c.sendMessage(str);
	}

	public void addClient(Client c)
	{
		clients.addElement(c);
	}

	public void removeClient(Client c)
	{
		clients.remove(c);
	}

	public void Log(String string)
	{
		textArea.append(string + "\n");
	}

	public void setServerSocket(int port)
	{

		try
		{
			serverSocket = new ServerSocket(port);
			textArea.append("Server-Socket created in " + port);
			textArea.append("\nWaiting Client.\n");
		}
		catch (IOException e)
		{
			textArea.append("Failed in Set ServerSocket");
		}

		while (true)
		{
			try
			{
				socket = serverSocket.accept();
				new Client(socket, this).start();
				
			}
			catch (IOException e)
			{
				textArea.append("Failed in connect to Clients\n");
			}
		}
	}

	public String getLocationExpect(Client me)
	{
		String str = "";
		Client client;
		for (int i = 0; i < clients.size(); i++)
		{
			client = clients.elementAt(i);
			if (me.equals(client))
				continue;
			str += "/@   " + client.getIndex() + "," + client.getLocation() + "\n";
		}
		return str;
	}

	public String getLocationAll()
	{
		String str = "";
		Client client;
		for (int i = 0; i < clients.size(); i++)
		{
			client = clients.elementAt(i);
			str += "/@   " + client.getIndex() + "," + client.getLocation() + "\n";
		}
		return str;

	}

	public static int LISTEN_PORT = 5777;
	public static final String PROGRAM_TITLE = "LBS Server";

	public static void main(String args[])
	{

		JFrame frame = new JFrame(PROGRAM_TITLE);

		Server server = new Server();
		frame.getContentPane().add(server);

		frame.pack();
		frame.setVisible(true);
		frame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});

		if (args.length > 0)
		{
			try
			{
				LISTEN_PORT = Integer.parseInt(args[0]);
			}
			catch (NumberFormatException e)
			{
				System.out.println("Usage: java ChattingServer [portnumber:default=" + LISTEN_PORT + "]");
				System.exit(-1);
			}
		}
		server.setServerSocket(LISTEN_PORT);
	}

}
